//
//  FindTableViewCell.m
//  SXH
//
//  Created by 房 国生 on 15/6/25.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "FindTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation FindTableViewCell

- (void)awakeFromNib {
    // Initialization code
    self.backgroundColor = LIGHTGARY;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (IBAction)shareAction:(id)sender {
    
    UIButton *btn = (UIButton *)sender;
    [_delegate shareAction:btn.tag];
}

- (IBAction)zanAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    [_delegate zanAction:btn.tag];
}

- (IBAction)enterZanListVC:(id)sender {
    UIButton *btn = (UIButton *)sender;
    [_delegate enterZanListVC:btn.tag];
}

- (IBAction)enterUserProfileVC:(id)sender {
    UIButton *btn = (UIButton *)sender;
    [_delegate enterUserProfileVC:btn.tag];
}
- (IBAction)enterReportVC:(id)sender {
    UIButton *btn = (UIButton *)sender;
    [_delegate enterReportVC:btn.tag];
}
- (IBAction)guanzhuAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    [_delegate guanzhuAction:btn.tag];
}

- (void)setFindEntity:(FindEntity *)findEntity
{
    NSArray *array = [findEntity.images_ componentsSeparatedByString:@","];
    NSArray *widthArray = [findEntity.img_width_ componentsSeparatedByString:@","];
    NSArray *heightArray = [findEntity.img_height_ componentsSeparatedByString:@","];
    if ([array count] == 0) {
        array = [findEntity.images componentsSeparatedByString:@","];
        widthArray = [findEntity.img_width componentsSeparatedByString:@","];
        heightArray = [findEntity.img_height componentsSeparatedByString:@","];
    }
    if (array != nil && [array count] > 0)
    {
        CGFloat totalHeight = 64;
        for (int i = 0; i < [array count]; i ++)
        {
            CGFloat width = [[widthArray objectAtIndex:i] floatValue];
            CGFloat height = [[heightArray objectAtIndex:i] floatValue];
            NSString *imageUrl = [NSString stringWithFormat:@"%@/%@", IMAGE_URL, [array objectAtIndex:i]];
            if (i == 0)
            {
                if (!_FirstImageView) {
                    self.FirstImageView = [[UIImageView alloc]init];
                    [self addSubview:self.FirstImageView];
                    [self.FirstImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(@4);
                        make.top.equalTo(@(totalHeight));
                        make.height.equalTo(@((SCREEN_WIDTH - 8)*height/width));
                        make.width.equalTo(@(SCREEN_WIDTH - 8));
                    }];
                    self.FirstImageView.tag = i;
                }
                [self.FirstImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
                 {
                     
                 }];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAtIndex:)];
                [self.FirstImageView addGestureRecognizer:tap];
                self.FirstImageView.userInteractionEnabled = YES;

            }
            else
            {
                UIImageView *imageView = [self viewWithTag:i];
                if (!imageView) {
                    imageView = [[UIImageView alloc]init];
                }
                [self addSubview:imageView];
                [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(@4);
                    make.top.equalTo(@(totalHeight));
                    make.height.equalTo(@((SCREEN_WIDTH - 8)*height/width));
                    make.width.equalTo(@(SCREEN_WIDTH - 8));
                }];
                imageView.tag = i;
                [imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
                 {
                     
                 }];
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAtIndex:)];
                [imageView addGestureRecognizer:tap];
                imageView.userInteractionEnabled = YES;
            }
            totalHeight += (SCREEN_WIDTH - 8)*height/width + 2;
        }
    }
}

- (void)tapAtIndex:(UITapGestureRecognizer*)tap;
{
    UIImageView *imageView = (UIImageView*)tap.view;
    if ([_delegate respondsToSelector:@selector(clickAtImage:)]) {
        [_delegate clickAtImage:imageView];
    }
}
@end
